package com.example.driving.ui.adapter;


import android.annotation.SuppressLint;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.driving.R;
import com.example.driving.base.App;
import com.example.driving.model.ExamErrorsBean;
import com.example.driving.model.ExamQuestionBean;
import com.example.driving.ui.view.ViewLearnChoose;
import com.example.driving.utils.ImageLoadUtils;
import com.example.driving.utils.SpannableWrap;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class QuestionListAdapter extends BaseQuickAdapter<ExamQuestionBean, BaseViewHolder> {
    private boolean isLearn;
    public static int right_count = 0;
    public static int wrong_count = 0;
    private ExamErrorsBean errorVo = new ExamErrorsBean();
    private HashMap<Integer, String> userAnswer = new HashMap<>();
    public QuestionListAdapter(@Nullable List<ExamQuestionBean> data) {
        super(R.layout.item_question, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, ExamQuestionBean questionVo) {

        // 填充题目
        TextView tv_question = baseViewHolder.getView(R.id.tv_question);

        //填充题目类型
        if (questionVo.getQuestionType() == 1) {
            SpannableWrap.setText(" ")
                    .image(R.mipmap.bm_practise_panduanti_day)
                    .append(questionVo.getQuestion())
                    .into(tv_question);
        } else if (questionVo.getQuestionType() == 2) {
            SpannableWrap.setText(" ")
                    .image(R.mipmap.bm_practise_danxuanti_day)
                    .append(questionVo.getQuestion())
                    .into(tv_question);
        } else if (questionVo.getQuestionType() == 3){
            SpannableWrap.setText(" ")
                    .image(R.mipmap.bm_practise_duoxuanti_day)
                    .append(questionVo.getQuestion())
                    .into(tv_question);
        }


        ImageView pic = baseViewHolder.getView(R.id.iv_pic);
        TextView tv_answer = baseViewHolder.getView(R.id.tv_answer);
        TextView tv_explain = baseViewHolder.getView(R.id.tv_explain);
        TextView tv_explain_title = baseViewHolder.getView(R.id.tv_explain_title);
        //填充解析
        baseViewHolder.setText(R.id.tv_explain, questionVo.getExplain());

        if (TextUtils.isEmpty(questionVo.getPic())) {//判断图片地址是不是空的
            pic.setVisibility(View.GONE);
        } else {
            pic.setVisibility(View.VISIBLE);
            ImageLoadUtils.getInstance().displayImage(pic.getContext(), R.mipmap.default_loadimage,
                    questionVo.getPic(), pic);
        }

        //填充四个选项的数据
        ViewLearnChoose viewLearnChoose = baseViewHolder.getView(R.id.mViewLearnChoose);
        viewLearnChoose.setData(questionVo, "", isLearn);
        viewLearnChoose.setOnAnswerClickListener(userPick -> {
            ansewrClick.onClick(userPick, questionVo);
            viewLearnChoose.setTheme();
            viewLearnChoose.setMultItem();
            String trueAnswer = questionVo.getAnswer();
            // 判断题
            if ("对".equals(trueAnswer)) {
                trueAnswer = "A";
            }
            if ("错".equals(trueAnswer)) {
                trueAnswer = "B";
            }
            if (trueAnswer.equals(userPick)) {
                right_count++;
            } else {
                wrong_count++;
            }
        });
        //必须先填充答案的数据
        setQuestionAnswer(tv_answer, questionVo, "");
        Set<Integer> set = userAnswer.keySet();

        //用户未选择任何一题的答案则不显示答案解析等
        if (set.size() == 0) {
            tv_answer.setVisibility(View.GONE);
            tv_explain.setVisibility(View.GONE);
            tv_explain_title.setVisibility(View.GONE);
        }



        for (Integer id : set) {
            //用户当前题未选择答案则不显示答案解析等
            if (TextUtils.isEmpty(userAnswer.get(questionVo.getId()))) {
                tv_answer.setVisibility(View.GONE);
                tv_explain.setVisibility(View.GONE);
                tv_explain_title.setVisibility(View.GONE);
            } else if (id == questionVo.getId()) {
                //显示答案
                viewLearnChoose.setAnswer(questionVo.getAnswer(), userAnswer.get(id), isLearn);
                // 选择完之后填充正确答案以及用户选择的答案
                setQuestionAnswer(tv_answer, questionVo, userAnswer.get(id));
                tv_answer.setVisibility(View.VISIBLE);
                tv_explain.setVisibility(View.VISIBLE);
                tv_explain_title.setVisibility(View.VISIBLE);
            }
        }
        if (questionVo.getQuestionType() == 3){
            tv_answer.setVisibility(View.GONE);
            tv_explain.setVisibility(View.GONE);
            tv_explain_title.setVisibility(View.GONE);
            return;
        }

        //背题模式则直接显示答案解析等
        if (isLearn) {
            tv_answer.setVisibility(View.VISIBLE);
            tv_explain.setVisibility(View.VISIBLE);
            tv_explain_title.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 设置练习模式
     * @param isLearn
     */
    @SuppressLint("NotifyDataSetChanged")
    public void setMode(boolean isLearn) {
        this.isLearn = isLearn;
        notifyDataSetChanged();
    }


    /**
     * 设置答案
     * @param textView
     * @param item
     * @param pick
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void setQuestionAnswer(TextView textView, ExamQuestionBean item, String pick) {
        int text_primary = App.getmContext().getResources().getColor(R.color.ang_text_base);
        int text_primary_color1 = App.getmContext().getResources().getColor(R.color.ang_09A6FE);
        int text_primary_color2 = App.getmContext().getResources().getColor(R.color.ang_FE6063);
        String trueAnswer = item.getAnswer();
        if ("对".equals(trueAnswer)) {
            trueAnswer = "A";
        }
        if ("错".equals(trueAnswer)) {
            trueAnswer = "B";
        }
        boolean b = (trueAnswer.endsWith(pick));
        if (TextUtils.isEmpty(pick) && isLearn) {
            SpannableWrap.setText("答案 ")
                    .textColor(text_primary)
                    .append(trueAnswer)
                    .textColor(text_primary_color1)
                    .into(textView);
        } else if (!TextUtils.isEmpty(pick) && !isLearn){
            SpannableWrap.setText("答案 ")
                    .textColor(text_primary)
                    .append(trueAnswer)
                    .textColor(text_primary_color1)
                    .append("  您选择 ")
                    .textColor(text_primary)
                    .append(pick)
                    .textColor(b ? text_primary_color1 : text_primary_color2)
                    .into(textView);
        }

    }


    /**
     * 用户答案
     * @param userAnswer
     */
    @SuppressLint("NotifyDataSetChanged")
    public void setUserAnswer(HashMap<Integer, String> userAnswer) {
        this.userAnswer = userAnswer;
        notifyDataSetChanged();
    }

    private OnAnswerClickListener ansewrClick;

    public interface OnAnswerClickListener {
        void onClick(String userPick, ExamQuestionBean questionVo);
    }

    public void setOnAnswerClickListener(OnAnswerClickListener ansewrClick) {
        this.ansewrClick = ansewrClick;
    }

}
